package com.jiangyg.mall.core.utils.time;

import java.util.concurrent.atomic.AtomicLong;

/**
 * 类描述：系统工具
 * 
 * @author jiangyg
 * @date 2019-10-11
 * @version 2.0
 */
public class ClockUtils {

    /**
     * 刷新频率
     */
    private final long rate = 10;

    /**
     * 实例
     */
    private static final ClockUtils instance = new ClockUtils();

    /**
     * 当前时间
     */
    private static volatile AtomicLong now;

    private ClockUtils() {
        now = new AtomicLong(System.currentTimeMillis());
        Thread tt = new Thread(() -> {
            while (true) {
                try {
                    Thread.sleep(rate);
                } catch (Exception ignored) {
                }
                now = new AtomicLong(System.currentTimeMillis());
            }
        });
        tt.setDaemon(true);
        tt.start();
    }

    public static ClockUtils instance() {
        return instance;
    }

    /**
     * 功能描述：获取当前时间戳
     * 
     * @return 当前时间戳
     */
    public static long currentTimeMillis() {
        return now.longValue();
    }

}
